package com.yellowpage.bigdata.service;

import java.util.List;
import java.util.Map;

import com.yellowpage.bigdata.dao.TmApponoffImeiChlRetainDao;
import com.yellowpage.bigdata.data.TmApponoffImeiChlRetain;
import com.yellowpage.white.common.PageRender;
import com.yellowpage.white.dao.QueryAssistantSQL;
import com.yellowpage.white.service.BaseServiceImpl;

public class TmApponoffImeiChlRetainServiceImpl extends BaseServiceImpl
		implements TmApponoffImeiChlRetainService {
	private TmApponoffImeiChlRetainDao tmApponoffImeiChlRetainDao;

	public void setTmApponoffImeiChlRetainDao(
			TmApponoffImeiChlRetainDao tmApponoffImeiChlRetainDao) {
		this.tmApponoffImeiChlRetainDao = tmApponoffImeiChlRetainDao;
	}

	/* Smooth generate Method */
	public TmApponoffImeiChlRetain getTmApponoffImeiChlRetain(Long id) {
		return tmApponoffImeiChlRetainDao.get(id);
	}

	public List<TmApponoffImeiChlRetain> getAllTmApponoffImeiChlRetain() {
		return tmApponoffImeiChlRetainDao.getAll();
	}

	public List<Map> queryTmApponoffImeiChlRetainByPage(Map queryMap,
			PageRender pageRender) {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT a.SHOW_DATE,a.CHANNEL_NO,(select b.NAME from OMC_DIC_TABLE b where b.VALUE = a.CHANNEL_NO and b.DIC_NAME='NSP_ONOFFLOG_CHANNEL_NO') as CHANNEL_NO_NAME,a.IMEI_NEW_COUNT,"
				+ " ROUND(SUM(a.NEXT_DAY_RETAINED)*100,3) AS NEXT_RETAINED ,"
				+ " ROUND(SUM(a.THREE_DAY_RETAINED)*100,3) AS THREE_RETAINED , "
				+ " ROUND(SUM(a.SEVEN_DAY_RETAINED)*100,3) AS SEVEN_RETAINED , "
				+ " ROUND(SUM(a.THIRTY_DAY_RETAINED)*100,3) AS THIRTY_RETAINED "
				+ " FROM `TM_APPONOFF_IMEI_CHL_RETAIN` a  WHERE");

		if (!"".equals(queryMap.get("showDate"))) {
			sql.append(" a.SHOW_DATE>=" + queryMap.get("showDate"));
		}
		if (!"".equals(queryMap.get("query_showDate_end"))) {
			sql.append(" AND a.SHOW_DATE<="
					+ queryMap.get("query_showDate_end"));
		}
		if (!"".equals(queryMap.get("channelNo"))) {
			sql.append(" AND a.CHANNEL_NO = " + queryMap.get("channelNo"));
		}

		sql.append(" GROUP BY a.SHOW_DATE,a.CHANNEL_NO");
		
		QueryAssistantSQL sqlAssistant = baseDao.newQueryAssistantSQL(sql
				.toString());

		List<Map> resultList = sqlAssistant.queryByPage(
				baseDao.getConnection(), queryMap, pageRender);

		return resultList;
	}

	public List<Map> queryTmApponoffImeiChlRetainByPage(Map queryMap) {

		StringBuffer sql = new StringBuffer();
		sql.append("SELECT a.SHOW_DATE,a.CHANNEL_NO,(select b.NAME from OMC_DIC_TABLE b where b.VALUE = a.CHANNEL_NO and b.DIC_NAME='NSP_ONOFFLOG_CHANNEL_NO') as CHANNEL_NO_NAME,a.IMEI_NEW_COUNT,"
				+ " ROUND(SUM(a.NEXT_DAY_RETAINED)*100,3) AS NEXT_RETAINED ,"
				+ " ROUND(SUM(a.THREE_DAY_RETAINED)*100,3) AS THREE_RETAINED , "
				+ " ROUND(SUM(a.SEVEN_DAY_RETAINED)*100,3) AS SEVEN_RETAINED , "
				+ " ROUND(SUM(a.THIRTY_DAY_RETAINED)*100,3) AS THIRTY_RETAINED "
				+ " FROM `TM_APPONOFF_IMEI_CHL_RETAIN` a  WHERE");

		if (!"".equals(queryMap.get("showDate"))) {
			sql.append(" a.SHOW_DATE>=" + queryMap.get("showDate"));
		}
		if (!"".equals(queryMap.get("query_showDate_end"))) {
			sql.append(" AND a.SHOW_DATE<="
					+ queryMap.get("query_showDate_end"));
		}
		if (!"".equals(queryMap.get("channelNo"))) {
			sql.append(" AND a.CHANNEL_NO = " + queryMap.get("channelNo"));
		}

		sql.append(" GROUP BY a.SHOW_DATE,a.CHANNEL_NO");
		QueryAssistantSQL sqlAssistant = baseDao.newQueryAssistantSQL(sql
				.toString());

		List<Map> resultList = sqlAssistant.query(baseDao.getConnection(),
				queryMap);

		return resultList;
	}

	public void saveTmApponoffImeiChlRetain(
			TmApponoffImeiChlRetain tmApponoffImeiChlRetain) {
		tmApponoffImeiChlRetainDao.save(tmApponoffImeiChlRetain);
	}

	public void updateTmApponoffImeiChlRetain(
			TmApponoffImeiChlRetain tmApponoffImeiChlRetain) {
		tmApponoffImeiChlRetainDao.update(tmApponoffImeiChlRetain);
	}

	public void delete(List deleteList) {
		tmApponoffImeiChlRetainDao.remove(deleteList);
	}

}